package com.enterprise.support.utility.generator.panel;

import com.enterprise.support.utility.generator.pub.GeneratorConfig;
import com.enterprise.support.utility.generator.pub.FreemarkerManager;
import com.enterprise.application.swing.jeta.AbstractPanel;
import com.enterprise.support.utility.Enterprise;
import com.enterprise.support.utility.SwingUtility;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-31
 * Time: 15:26:54
 * To change this template use File | Settings | File Templates.
 */
public class TabHiberante extends AbstractPanel {
    private final static File srcDirectory = new File(com.enterprise.support.utility.Configuration.LibDirectory, "/java/src");
    private final static File comDirectory = new File(srcDirectory, "com");
    private final static DefaultTableModel tableModel = new DefaultTableModel(new Vector() {{
        add("字段名");
        add("类型");
    }}, 0);

    public TabHiberante() {
        super("com\\enterprise\\support\\utility\\generator\\panel\\TabHiberante.xml");
    }

    public void initComponents() {
        mainPanel.getComboBox("projects").setModel(new DefaultComboBoxModel(new Vector<Object>() {{
            File[] files = comDirectory.listFiles();
            if (null != files) {
                for (File project : files) {
                    if (project.isDirectory()) {
                        add(project.getName());
                    }
                }
            }
        }}));
        mainPanel.getComboBox("modules").setModel(new DefaultComboBoxModel(new Vector<Object>() {{
            File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application");
            File[] files = moduleDirectory.listFiles();
            if (null != files) {
                for (File module : files) {
                    if (module.isDirectory()) {
                        add(module.getName());
                    }
                }
            }
        }}));

        super.mainPanel.getTable("propertys").setModel(tableModel);
        super.mainPanel.getComboBox("type").setModel(new DefaultComboBoxModel(new Vector<Object>() {{
            add(" String");
            add(" Integer");
            add(" Long");
            add(" Float");
            add(" Double");
            add(" Boolean");
            add(" Date");
        }}));

    }

    public void addListeners() {
        mainPanel.getComboBox("projects").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mainPanel.getComboBox("modules").setModel(new DefaultComboBoxModel(new Vector<Object>() {{
                    File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application");
                    File[] files = moduleDirectory.listFiles();
                    if (files != null) {
                        for (File module : files) {
                            if (module.isDirectory()) {
                                add(module.getName());
                            }
                        }
                    }
                }}));
            }
        });
        super.mainPanel.getButton("add").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                final Enterprise input = getInput();
                if (input.isEmpty("field")) {
                    SwingUtility.warning("字段名为必填！");
                } else if (!input.getString("field").matches("^[a-zA-Z].*")) {
                    SwingUtility.warning("字段名必须以字母开头！");
                } else {
                    for (Object values : tableModel.getDataVector()) {
                        if (input.equals("field", ((Vector) values).get(0))) {
                            SwingUtility.warning(input.getString("field") + " 字段已存在！");
                            return;
                        }
                    }
                    Vector value = new Vector();
                    value.add(input.getString("field").toLowerCase().trim());
                    value.add(input.getString("type").trim());
                    tableModel.addRow(value);
                    mainPanel.getTextField("field").setText("");
                }
            }
        });
        super.mainPanel.getButton("delete").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Integer index = mainPanel.getTable("propertys").getSelectedRow();
                if (index > -1) {
                    tableModel.removeRow(index);
                } else {
                    SwingUtility.warning("请选择要删除的记录!");
                }
            }
        });

        super.mainPanel.getButton("submit").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                Enterprise input = getInput();
                if (input.isEmpty("modules")) {
                    SwingUtility.warning("请选择模块！");
                } else if (input.isEmpty("classname")) {
                    SwingUtility.warning("类名为必填!");
                } else if (!input.getString("classname").matches("^[a-zA-Z].*")) {
                    SwingUtility.warning("类名必须以字母开头！");
                } else if (input.getString("classname").toLowerCase().indexOf("entity") != -1) {
                    SwingUtility.warning("请去除 实体类 名称中的 Entity 关键字！");
                } else if (!input.isEmpty("tablename") && !input.getString("tablename").matches("^[a-zA-Z].*")) {
                    SwingUtility.warning("表名必须以字母开头！");
                } else if (tableModel.getRowCount() < 1) {
                    SwingUtility.warning("请输入至少一个字段！");
                } else {
                    boolean flag = true;
                    for (Object values : tableModel.getDataVector()) {
                        if (((Vector) values).get(0).toString().equalsIgnoreCase("id")) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        Vector value = new Vector();
                        value.add("id");
                        value.add("Integer");
                        tableModel.addRow(value);
                    }

                    input.set("date", new Date());
                    input.set("classname", input.getString("classname").trim().toLowerCase());
                    input.set("entityName", input.getString("classname").replaceFirst("[a-z]", input.getString("classname").substring(0, 1).toUpperCase()) + "Entity");

                    File domainDirectory = new File(GeneratorConfig.homeDirectory, "/java/src/com/" + input.getString("projects") + "/application/" + input.getString("modules") + "/domain/");
                    String classPath = new File(domainDirectory, input.getString("entityName") + ".java").getAbsolutePath();
                    input.set("dbtype", GeneratorConfig.getProperties(GeneratorConfig.jdbcProperties).getProperty("dbpool.dbtype").toLowerCase().trim());
                    FreemarkerManager.builerTemplate("Entity.ftl", input, classPath);

                    mainPanel.getTextField("classname").setText("");
                    mainPanel.getTextField("tablename").setText("");
                    mainPanel.getTextField("field").setText("");
                    tableModel.getDataVector().removeAllElements();
                    tableModel.getDataVector().addAll(new Vector());
                    SwingUtility.warning("实体对象已生成！");
                }
            }
        });
    }
}
